package com.ruoyi.blog.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.blog.domain.TBlog;
import com.ruoyi.blog.domain.TCategory;
import com.ruoyi.blog.service.ITCategoryService;
import com.ruoyi.common.core.constant.UserConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 类型Controller
 * 
 * @author ruoyi
 * @date 2021-11-14
 */
@RestController
@RequestMapping("/category")
public class TCategoryController extends BaseController
{
    @Autowired
    private ITCategoryService tCategoryService;

    /**
     * 查询类型列表
     */
    @RequiresPermissions("blog:category:list")
    @GetMapping("/list")
    public TableDataInfo list(TCategory tCategory)
    {
//        startPage();
        List<TCategory> list = tCategoryService.selectTCategoryList(tCategory);
//        return getDataTable(list);
        return getDataTable(tCategoryService.buildCategoryTreeSelect(list));
    }

    /**
     * 导出类型列表
     */
    @RequiresPermissions("blog:category:export")
    @Log(title = "类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCategory tCategory) throws IOException
    {
        List<TCategory> list = tCategoryService.selectTCategoryList(tCategory);
        ExcelUtil<TCategory> util = new ExcelUtil<TCategory>(TCategory.class);
        util.exportExcel(response, list, "类型数据");
    }

    /**
     * 获取类型详细信息
     */
    @RequiresPermissions("blog:category:query")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return AjaxResult.success(tCategoryService.selectTCategoryByCategoryId(categoryId));
    }

    /**
     * 新增类型
     */
    @RequiresPermissions("blog:category:add")
    @Log(title = "类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCategory tCategory)
    {
        if (UserConstants.NOT_UNIQUE.equals(tCategoryService.checkCategoryNameUnique(tCategory))){
            return AjaxResult.error("新增部门'" + tCategory.getText() + "'失败，分类名称已存在");
        }
        return toAjax(tCategoryService.insertTCategory(tCategory));
    }

    /**
     * 修改类型
     */
    @RequiresPermissions("blog:category:edit")
    @Log(title = "类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCategory tCategory)
    {
        return toAjax(tCategoryService.updateTCategory(tCategory));
    }

    /**
     * 删除类型
     */
    @RequiresPermissions("blog:category:remove")
    @Log(title = "类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        for(int i = 0; i < categoryIds.length; i++){
            if (tCategoryService.hasChildByCategoryId(categoryIds[i]))
            {
                return AjaxResult.error("存在下级类型,不允许删除");
            }
            if (tCategoryService.checkCategoryExistBlog(categoryIds[i]))
            {
                return AjaxResult.error("该类型有博客,不允许删除");
            }
        }
        return toAjax(tCategoryService.deleteTCategoryByCategoryIds(categoryIds));
    }

    /**
     * 前端查询列表
     * @return
     */
    @GetMapping("/fromIndex/list")
    public TableDataInfo listCategoeyFromIndex(){
        TCategory tCategory = new TCategory();
        tCategory.setStatus("0");
        return getDataTable(tCategoryService.selectTCategoryList(tCategory));
    }
}
